/* Copyright 2008. All rights reserved. */
package org.bling.parser.tags;

import org.bling.parser.BlingParserException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Widget;

/**
 * renders an SWT Menu
 * 
 * @author DISBROB
 * 
 */
public class MenuTag extends Tag {

	protected Menu menu;

	@Override
	public int doStartTag() throws BlingParserException {
		super.doStartTag();

		Shell shell = context.getShell();

		if (parent.getWidget() instanceof Shell) {
			menu = new Menu(shell, SWT.BAR);
			shell.setMenuBar(menu);
		}

		if (parent.getWidget() instanceof MenuItem) {
			MenuItem menuItem = (MenuItem) parent.getWidget();
			menu = new Menu(shell, SWT.DROP_DOWN);
			// menuItem = new MenuItem((Menu) parent.parent.getWidget(),
			// SWT.CASCADE);
			menuItem.setMenu(menu);
		}

		if (menu == null) {
			throw new BlingParserException("Invalid location of Menu tag. ");
		}

		return EVAL_PAGE;
	}

	@Override
	public Widget getWidget() throws BlingParserException {
		return menu;
	}

}
